package com.ttice.icewkment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttice.icewkment.commin.vo.SquareVO;
import com.ttice.icewkment.commin.vo.UserPageVO;
import com.ttice.icewkment.commin.vo.UserVO;
import com.ttice.icewkment.entity.User;
import com.ttice.icewkment.mapper.UserMapper;
import com.ttice.icewkment.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-01-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User GetUserInfoById(Integer id) {
        //根据用户id获取名称信息
        //id是内容发布者id
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public UserPageVO VoList(Integer page, Integer limit) {

        List<UserVO> result = new ArrayList<>();
        UserVO userVO = null;
        Page<User> userPage = new Page<>(page,limit);
        QueryWrapper<User> wrapper= new QueryWrapper<User>();
        wrapper.orderByDesc("integral");
        Page<User> resultPage = userMapper.selectPage(userPage, wrapper);

        List<User> users = resultPage.getRecords();
        for (User user : users) {
            userVO = new UserVO();
            userVO.setUserId(user.getUserId());
            userVO.setUserAge(user.getUserAge());
            userVO.setProfile(user.getProfile());
            userVO.setIntro(user.getIntro());
            userVO.setGender(user.getGender());
            userVO.setUsername(user.getUsername());
            result.add(userVO);
        }
        UserPageVO userPageVO = new UserPageVO();
        userPageVO.setData(result);
        userPageVO.setTotal(resultPage.getTotal());
        userPageVO.setPages(resultPage.getPages());
        return userPageVO;
    }
}
